package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.MarketCheckInventoryRecordService
import com.zxy.supplier_system.server.utils.GoodsExcelField
import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.utils.NullableLocalDateRange
import org.springframework.core.io.InputStreamResource
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime

@RestController
@RequestMapping("markets/checkInventoryRecords")
class MarketCheckInventoryRecordController(
    private val marketCheckInventoryRecordService: MarketCheckInventoryRecordService,
) {

    data class CreateMarketCheckInventoryRecordRequest(
        val name: String?,
        val marketId: Int
    )

    @HasAnyAuthority(Authority.卖场_盘点_盘点)
    @PostMapping
    fun createMarketCheckInventoryRecord(@RequestBody request: CreateMarketCheckInventoryRecordRequest): MarketCheckInventoryRecordController.QueryMarketCheckInventoryRecordResponse {
        return this.marketCheckInventoryRecordService.createMarketCheckInventoryRecord(request)
    }

    class QueryMarketCheckInventoryRecordRequest(
        val startDate: LocalDate?,
        val endDate: LocalDate?,
        val employeeIds: List<Int> = emptyList(),
        val submitted: Boolean?,
        val marketIds: List<Int> = emptyList(),
    )

    @HasAuthority(Authority.卖场_盘点_查询)
    @GetMapping
    fun queryMarketCheckInventoryRecord(
        queryMarketCheckInventoryRecordRequest: QueryMarketCheckInventoryRecordRequest,
        pageable: Pageable
    ): Page<QueryMarketCheckInventoryRecordResponse> {
        return this.marketCheckInventoryRecordService.queryMarketCheckInventoryRecord(
            queryMarketCheckInventoryRecordRequest,
            pageable
        )
    }

    data class QueryMarketCheckInventoryRecordResponse(
        val id: Int,
        val createdBy: IntIdAndName,
        val createdDateTime: OffsetDateTime,
        val goodsKindAmount: Int,
        val name: String,
        val submittedDateTime: OffsetDateTime?,
        val market: IntIdAndName,
    )

    data class UpdateOrderGoodsRequest(
        val items: List<OrderGoodsItem>
    )

    data class OrderGoodsItem(
        val id: Int,
        val amount: Int
    )

    @HasAuthority(Authority.卖场_盘点_盘点)
    @PatchMapping("{id}/goods")
    fun updateOrderGoods(
        @RequestBody updateOrderGoodsRequest: UpdateOrderGoodsRequest, @PathVariable id: Int,
    ) {
        return this.marketCheckInventoryRecordService.updateOrderGoods(id, updateOrderGoodsRequest)
    }

    @HasAuthority(Authority.卖场_盘点_查询)
    @GetMapping("{id}")
    fun getMarketCheckInventoryRecordDetail(
        @PathVariable id: Int,
        includeItems: Boolean = false
    ): MarketCheckInventoryRecordDetail {
        return this.marketCheckInventoryRecordService.getMarketCheckInventoryRecordDetail(id, includeItems)
    }

    data class MarketCheckInventoryRecordDetail(
        val id: Int,
        val createdDateTime: OffsetDateTime,
        val name: String,
        val createdBy: IntIdAndName,
        val items: List<MarketCheckInventoryRecordItemResponse>,
        val submittedDateTime: OffsetDateTime?,
        val market: IntIdAndName,
    )

    data class MarketCheckInventoryRecordItemResponse(
        val id: Int,
        val name: String,
        val barcode: String,
        val price: BigDecimal,
        val amount: Int,
        val createdDateTime: OffsetDateTime,
        val lastModifiedDateTime: OffsetDateTime?,
        val brand: IntIdAndName?,
        val category: IntIdAndName?,
        val factory:IntIdAndName?,
        val amountInOrder: Int,
        val boxSpecification: String?,
        val registrationCertificateNumber: String?,
        val executiveStandard:String?,
        val recordFilingNumber:String?,
    )

    @HasAuthority(Authority.卖场_盘点_查询)
    @GetMapping("employees", params = ["projection=summary"])
    fun getMarketCheckInventoryRecordCreatedEmployees(): List<IntIdAndName> {
        return this.marketCheckInventoryRecordService.getAllEmployees()
    }

    @HasAuthority(Authority.卖场_盘点_查询)
    @GetMapping("markets", params = ["projection=summary"])
    fun getMarketCheckInventoryRecordCreatedMarkets(): List<IntIdAndName> {
        return this.marketCheckInventoryRecordService.getAllMarkets()
    }


    @HasAuthority(Authority.卖场_盘点_盘点)
    @PatchMapping("{id}/submit")
    fun submitOrder(@PathVariable id: Int) {
        return this.marketCheckInventoryRecordService.submitMarketCheckInventoryRecord(id)
    }

    @HasAuthority(Authority.卖场_盘点_查询)
    @GetMapping("createdDateRanges")
    fun getAllMarketCheckInventoryRecordCreatedDateRange(): NullableLocalDateRange {
        return this.marketCheckInventoryRecordService.getAllMarketCheckInventoryRecordCreatedDateRange()
    }

    class ExportToExcelRequest(
        val fields: Array<GoodsExcelField> = GoodsExcelField.values(),
    )

    @HasAnyAuthority(Authority.卖场_盘点_查询)
    @GetMapping("{id}/excel")
    fun exportToExcel(
        request:ExportToExcelRequest,
        @PathVariable id: Int
    ): InputStreamResource {
        return this.marketCheckInventoryRecordService.exportToExcel(id, request.fields)
    }

    @HasAnyAuthority(Authority.卖场_盘点_删除)
    @DeleteMapping("{id}")
    fun delete(@PathVariable id: Int){
        return this.marketCheckInventoryRecordService.delete(id)
    }

}
